import QtQuick 2.0
import cn.pset.ColorMarker 1.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
Window{
    visible: true
    width: 420; height: 420
    Rectangle{
        anchors.fill: parent
        Text {
            id: timeLabel
            anchors.left: parent.left
            anchors.leftMargin: 4
            anchors.top: parent.top
            anchors.topMargin: 4
            font.pixelSize: 26;
        }
        ColorMarker{
            id: colorMarker
            color: Qt.green
        }

        Rectangle{
            id: colorRect
            width: 320; height: 320
            anchors.centerIn: parent
            color: "blue"
            radius: 10
            border.width: 3
        }

        Button{
            id: startBtn
            text: "start"
            anchors.left: parent.left
            anchors.leftMargin: 4
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 4
            onClicked: {
                colorMarker.start();
                startBtn.enabled = false;
                stopBtn.enabled = true;
            }
            style: btnStyle
        }
        Button{
            id: stopBtn
            text: "stop"
            anchors.left: startBtn.right
            anchors.leftMargin: 4
            anchors.bottom: startBtn.bottom
            onClicked:  {
                colorMarker.stop();
                startBtn.enabled = true;
                stopBtn.enabled = false;
            }
            style: btnStyle
        }



        Button{
            id: colorAlgorithm
            text: "RandomRGB"
            anchors.left: stopBtn.right
            anchors.leftMargin: 4
            anchors.bottom: startBtn.bottom
            onClicked: {
                var algorithm = (colorMarker.algorithm()+1)%5;
                parent.changeAlgorithm(colorAlgorithm, algorithm);
                colorMarker.setAlgorithm(algorithm);
            }
            style: btnStyle
        }

        Button{
            id: quit
            text: "quit"
            anchors.left: colorAlgorithm.right
            anchors.leftMargin: 4
            anchors.bottom: startBtn.bottom
            onClicked: { Qt.quit()}
            style: btnStyle
        }

        function changeAlgorithm(button, algorithm){
            switch(algorithm){
            case ColorMarker.RandomRGB:
                button.text = "RandomRGB"
                break;
            case ColorMarker.RandomRed:
                button.text = "RandomRed"
                break;
            case ColorMarker.RandomGreen:
                button.text = "RandomGreen"
                break;
            case ColorMarker.RandomBlue:
                button.text = "RandomBlue"
                break;
            case ColorMarker.LinearIncrease:
                button.text = "LinearIncrease"
                break;
            }
        }

        Component{
            id: btnStyle
            ButtonStyle{
                background: Rectangle{
                    implicitHeight: 30; implicitWidth:90
                    radius: 15
                    border.color: "lightgreen"
                    border.width: 2
                }
            }
        }

        Component.onCompleted: {
            colorMarker.color = Qt.rgba(0, 180, 120, 255);
            colorMarker.setAlgorithm(ColorMarker.LinearIncrease);
            changeAlgorithm(colorAlgorithm, colorMarker.algorithm());
        }
        Connections{
            target: colorMarker
            onCurrentTime: {
                timeLabel.text = strTime;
                timeLabel.color = colorMarker.timeColor;
            }
        }

        Connections{
            target: colorMarker
            onColorChanged: {
                colorRect.color = color
            }
        }
    }
}


